<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/7/23
 * Time: 13:37
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Goods extends Base
{
    /**
     * 商品首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $keyword = trim(input('keyword'), ' ');
            $goods_first_cate = input('goods_first_cate');
            $goods_second_cate = input('goods_second_cate');
            $goods_status = input('goods_status');
            $goods_type = input('bg_type');
            $where['goods_status'] = ['<>', 3];
            // 关键字
            if(isset($keyword) && $keyword != ''){
                $where['goods_id|goods_name'] = ['like', '%' . $keyword . '%'];
            }
            // 一级分类
            if(isset($goods_first_cate) && $goods_first_cate != ''){
                $where['goods_first_cate'] = $goods_first_cate;
            }
            // 二级分类
            if(isset($goods_second_cate) && $goods_second_cate != ''){
                $where['goods_second_cate'] = ['LIKE', '%,' . $goods_second_cate . ',%'];
            }
            // 状态
            if(isset($goods_status) && $goods_status != ''){
                $where['goods_status'] = $goods_status;
            }
            if(isset($goods_type) && $goods_type != ''){
                $where['goods_type'] = $goods_type;
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_goods')
                ->join('up_goods_cate u1', 'u1.gc_id = goods_first_cate', 'LEFT')
                ->join('up_th_rule', 'rule_id = goods_th_rule', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_goods')
                ->join('up_goods_cate u1', 'u1.gc_id = goods_first_cate', 'LEFT')
                ->join('up_th_rule', 'rule_id = goods_th_rule', 'LEFT')
                ->field('goods_id, goods_name, goods_thumb, goods_price, goods_vip_price, goods_sale_num, 
                goods_status, goods_spec, goods_stock, goods_time, u1.gc_name first_cate_name, goods_second_cate, 
                goods_is_recommend, goods_sort, goods_type, rule_name, goods_first_cate, goods_tui')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('goods_sort desc, goods_time desc')
                ->select();
            if($lists){
                foreach ($lists as $k => $v){
                    $second_cate_ids = trim($v['goods_second_cate'], ',');
                    $second_cate = Db::table('up_goods_cate')->where('gc_id', 'IN', $second_cate_ids)->column('gc_name');
                    $lists[$k]['goods_second_cate'] = implode(',', $second_cate);
                }

            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $cate = Db::table('up_goods_cate')->where('gc_level', 1)->field('gc_id, gc_name')->select();
        $this->assign([
            'cate' => $cate
        ]);
        return $this->fetch();
    }
    
    /**
     * 添加商品
     * @return mixed|\think\response\Json
     */
    public function goodsAdd(){
        if(Request::instance()->isPost()){
            $goods_name = trim(input('goods_name'), ' ');
            $goods_thumb = input('goods_thumb');
            $goods_images = input('goods_images');
            $goods_status = input('goods_status');
            $goods_first_cate = input('goods_first_cate');
            $goods_second_cate = input('select');
            $goods_is_recommend = input('goods_is_recommend');
            $goods_price = input('goods_price');
            $goods_vip_price = input('goods_vip_price');
            $goods_spec = input('goods_spec');
            $goods_stock = input('goods_stock');
            $goods_product_code = trim(input('goods_product_code'));
//            $goods_is_show_sale_num = input('goods_is_show_sale_num');
            $goods_content = input('goods_content');
            $goods_sort = input('goods_sort') ?: 1;
            $goods_type = input('goods_type');
            $th_rule = input('th_rule') ? input('th_rule') : 0;

            if($goods_first_cate == ''){
                return json(['code' => 100, 'msg' => '请选择商品分类']);
            }
            if($goods_first_cate == 4 && !$th_rule){
                return json(['code' => 100, 'msg' => '请选择礼包权限']);
            }

            $has = Db::table('up_goods')->where('goods_product_code', $goods_product_code)->find();
            if($has) return json(['code' => 100, 'msg' => '产品码已存在']);

            if($goods_second_cate) $goods_second_cate = ',' . $goods_second_cate . ',';
            $insert = [
                'goods_name' => $goods_name,
                'goods_thumb' => $goods_thumb,
                'goods_content' => $goods_content,
                'goods_images' => $goods_images,
                'goods_status' => $goods_status,
                'goods_type' => $goods_type,
                'goods_first_cate' => $goods_first_cate,
                'goods_second_cate' => $goods_second_cate,
                'goods_is_recommend' => $goods_is_recommend,
                'goods_price' => $goods_price,
                'goods_vip_price' => $goods_vip_price,
                'goods_spec' => $goods_spec,
                'goods_stock' => $goods_stock,
                'goods_product_code' => $goods_product_code,
                'goods_sort' => $goods_sort,
                'goods_th_rule' => $th_rule,
//                'goods_is_show_sale_num' => $goods_is_show_sale_num,
                'goods_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_goods')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $cate = Db::table('up_goods_cate')->where('gc_level', 1)->field('gc_id, gc_name')->select();
        $thRule = Db::table('up_th_rule')->where('rule_status', 1)->field('rule_id, rule_name')->select();
        $this->assign([
            'cate' => $cate,
            'thRule' => $thRule
        ]);
        return $this->fetch('goods/goodsadd');
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function goodsDetail(){
        $goods_id = input('goods_id');
        $goods = Db::table('up_goods')
            ->join('up_goods_cate u1', 'u1.gc_id = goods_first_cate', 'LEFT')
            ->join('up_th_rule', 'goods_th_rule = rule_id', 'LEFT')
            ->field('goods_id, goods_name, goods_thumb, goods_price, goods_vip_price, goods_sale_num, 
                goods_status, goods_spec, goods_stock, goods_time, u1.gc_name first_cate_name,  
                goods_is_recommend, goods_images, goods_content, goods_is_show_sale_num, goods_is_show_stock, 
                goods_product_code, goods_type, goods_second_cate, rule_name, goods_first_cate')
            ->where('goods_id', $goods_id)
            ->find();
        if($goods['goods_images']){
            $goods['goods_images'] = explode(',', $goods['goods_images']);
        }else{
            $goods['goods_images'] = [];
        }
        $second_str = trim($goods['goods_second_cate'], ',');
        $second_cate_name = Db::table('up_goods_cate')->where('gc_id', 'IN', $second_str)->column('gc_name');
        $goods['second_cate_name'] = implode(',', $second_cate_name);
        return $this->fetch('goods/goodsdetail', ['goods' => $goods]);
    }
    
    /**
     * 编辑商品
     * @return mixed|\think\response\Json
     */
    public function goodsEdit(){
        $goods_id = input('goods_id');
        if(Request::instance()->isPost()){
            $goods_name = trim(input('goods_name'), ' ');
            $goods_thumb = input('goods_thumb');
            $goods_images = input('goods_images');
            $goods_status = input('goods_status');
            $goods_first_cate = input('goods_first_cate');
            $goods_second_cate = input('select');
            $goods_is_recommend = input('goods_is_recommend');
            $goods_price = input('goods_price');
            $goods_vip_price = input('goods_vip_price');
            $goods_spec = input('goods_spec');
            $goods_stock = input('goods_stock');
            $goods_sort = input('goods_sort') ?: 1;
            $goods_content = input('goods_content');
            $goods_is_show_sale_num = input('goods_is_show_sale_num');
            $goods_is_show_stock = input('goods_is_show_stock');
            $goods_product_code = trim(input('goods_product_code'));
            $goods_type = input('goods_type');
            $th_rule = input('th_rule') ? input('th_rule') : 0;

            if($goods_first_cate == ''){
                return json(['code' => 100, 'msg' => '请选择商品分类']);
            }
            if($goods_first_cate == 4){
                if(!$th_rule) return json(['code' => 100, 'msg' => '请选择礼包权限']);
            }else{
                $th_rule = 0;
            }

            $has = Db::table('up_goods')->where('goods_product_code', $goods_product_code)->find();
            if($has && $goods_id != $has['goods_id']) return json(['code' => 100, 'msg' => '产品码已存在']);

            if($goods_second_cate) $goods_second_cate = ',' . $goods_second_cate . ',';
            $update = [
                'goods_name' => $goods_name,
                'goods_thumb' => $goods_thumb,
                'goods_content' => $goods_content,
                'goods_images' => $goods_images,
                'goods_status' => $goods_status,
                'goods_first_cate' => $goods_first_cate,
                'goods_second_cate' => $goods_second_cate,
                'goods_is_recommend' => $goods_is_recommend,
                'goods_price' => $goods_price,
                'goods_vip_price' => $goods_vip_price,
                'goods_spec' => $goods_spec,
                'goods_stock' => $goods_stock,
                'goods_is_show_sale_num' => $goods_is_show_sale_num,
                'goods_is_show_stock' => $goods_is_show_stock,
                'goods_product_code' => $goods_product_code,
                'goods_sort' => $goods_sort,
                'goods_updatetime' => date('Y-m-d H:i:s'),
                'goods_type' => $goods_type,
                'goods_th_rule' => $th_rule
            ];
            $res = Db::table('up_goods')->where('goods_id', $goods_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $goods = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->field('goods_name, goods_thumb, goods_images, goods_status, goods_first_cate, goods_second_cate, 
            goods_is_recommend, goods_price, goods_vip_price, goods_spec, goods_stock, goods_content, goods_is_show_sale_num, 
            goods_is_show_stock, goods_product_code, goods_sort, goods_type, goods_th_rule')
            ->find();
        $first_cate = Db::table('up_goods_cate')->where('gc_level', 1)->field('gc_id, gc_name')->select();
        $second_str = trim($goods['goods_second_cate'], ',');
//        $second_cate = Db::table('up_goods_cate')->where('gc_pid', $goods['goods_first_cate'])->field('gc_id, gc_name')->select();
        $thRule = Db::table('up_th_rule')->where('rule_status', 1)->field('rule_id, rule_name')->select();
        $this->assign([
            'goods_id' => $goods_id,
            'goods' => $goods,
            'first_cate' => $first_cate,
//            'second_cate' => $second_cate,
            's_cate' => $second_str,
            'thRule' => $thRule
        ]);
        return $this->fetch('goods/goodsedit');
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function goodsOn(){
        $goods_id = input('id');
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 100, 'msg' => '上架失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function goodsDown(){
        $goods_id = input('id');
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 100, 'msg' => '下架失败']);
        }
    }
    
    /**
     * 设置推荐
     * @return \think\response\Json
     */
    public function goodsRecommend(){
        $goods_id = input('id');
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_is_recommend', 2);
        if($res){
            return json(['code' => 200, 'msg' => '设置推荐成功']);
        }else{
            return json(['code' => 100, 'msg' => '设置推荐失败']);
        }
    }
    
    /**
     * 取消推荐
     * @return \think\response\Json
     */
    public function goodsUnrecommend(){
        $goods_id = input('id');
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_is_recommend', 1);
        if($res){
            return json(['code' => 200, 'msg' => '取消推荐成功']);
        }else{
            return json(['code' => 100, 'msg' => '取消推荐失败']);
        }
    }
    
    /**
     * 删除商品
     * @return \think\response\Json
     */
    public function goodsDel(){
        $goods_id = input('id');
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    /**
     * 排序
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'goods_sort'=>input('value'),
        ];
        $res = Db::table('up_goods')->where('goods_id', $id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }

    /**
     * 推送
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function tui(){
        $goods_id = input('id');

        $goods = Db::table('up_goods')->where('goods_id', $goods_id)->field('goods_th_rule')->find();
        $userIds = Db::table('up_u_user')->where('user_th_rule', $goods['goods_th_rule'])->column('user_id');

        Db::startTrans();
        try{
            Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_tui', 2);

            if($userIds){
                // 推送
                $list = [];
                foreach($userIds as $k => $v){
                    $v1 = (string)$v;
                    $list[$k] = $v1;
                }
                $this->Jpush(1, $list, '您有一个待领取礼包', ['send_type' => 3]);
            }
            Db::commit();
            return json(['code' => 200, 'msg' => '操作成功']);
        }catch (\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => $e->getMessage()]);
        }
    }
}